﻿(function () {
    $(function () {

        var _$productsTable = $('#ProductsTable');
        var _productService = abp.services.app.product;

        //权限
        var _permissions = {
            create: abp.auth.hasPermission('Pages.Product.Create'),
            edit: abp.auth.hasPermission('Pages.Product.Edit'),
            'delete': abp.auth.hasPermission('Pages.Product.Delete')
        };

        var _createModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Mpa/Product/CreateModal',//加载视图
            scriptUrl: abp.appPath + 'Areas/Mpa/Views/Product/_CreateModal.js',//加载对应js
            modalClass: 'CreateProductModal'
        });

        var _editModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Mpa/Product/EditModal',
            scriptUrl: abp.appPath + 'Areas/Mpa/Views/Product/_EditModal.js',
            modalClass: 'EditProductModal'
        });

        _$productsTable.jtable({
            title: app.localize('ProductManager'),//标题
            paging: true,//启用分页
            sorting: true,//启用排序
            multiSorting: true,//启用多列排序
            actions: {
                listAction: {
                    method: _productService.getProductsAsync//获取列表方法
                }
            },
            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),//操作列
                    width: '15%',
                    sorting: false,
                    display: function (data) {
                        var $span = $('<span></span>');
                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function() {
                                    _editModal.open({ id: data.record.id });
                                });
                        }
                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function() {
                                    deleteProduct(data.record);
                                });
                        }
                        return $span;
                    }
                },
                name: {
                    title: app.localize('Product_Name'),
                    width: '20%'
                },
                price: {
                    title: app.localize('Product_Price'),
                    width:'20%'
                },
                categoryName: {
                    title: app.localize('Product_CategoryName'),
                    width: '20%',
                    sorting: false
                }
            }
        });
        //获取列表
        function getProducts(reload) {
            if (reload) {
                _$productsTable.jtable('reload');
            } else {
                _$productsTable.jtable('load', {
                    categoryId: $('#CategoryId').val(),
                    name: $("#Name").val(),
                    startPrice: $("#StartPrice").val(),
                    endPrice:$("#EndPrice").val()
                });
            }
        }

        //删除分类
        function deleteProduct(product) {
            abp.message.confirm(
                app.localize('ProductDeleteWarningMessage', product.name),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _productService.deleteProductAsync({
                            id: product.id
                        }).done(function () {
                            getProducts();
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }


        //添加点击事件
        $('#CreateNewProductButton').click(function () {
            _createModal.open();
        });

        //搜索点击事件
        $('#GetProductButton').click(function (e) {
            //取消事件的默认动作
            e.preventDefault();
            getProducts();
        });

        //搜索点击事件
        $("#ClearProductButton").click(function (e) {
            e.preventDefault();
            $('#CategoryId').val("");
            $("#Name").val("");
            $("#StartPrice").val("");
            $("#EndPrice").val("");
            getProducts();
        });

        //事件注册
        abp.event.on('app.createProductModalSaved', function () {
            getProducts(true);
        });
        abp.event.on('app.editProductModalSaved', function () {
            getProducts(true);
        });
        //页面加载完执行
        getProducts();
    });
})();